package com.skyvin.nreceipts.admin.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyvin.nreceipts.admin.mongorepository.ItemCategoryRepository;
import com.skyvin.nreceipts.admin.mongorepository.ItemCategoryTemplateRepository;
import com.skyvin.nreceipts.common.document.AppUser;
import com.skyvin.nreceipts.common.document.ReceiptCategory;
import com.skyvin.nreceipts.common.document.ReceiptItemCategory;
import com.skyvin.nreceipts.common.document.ReceiptItemCategoryTemplate;
import com.skyvin.nreceipts.common.service.BaseService;
import com.skyvin.nreceipts.user.service.UserService;

@Service
public class ReceiptItemCategoryService extends BaseService {
	private static Logger logger = Logger.getLogger(ReceiptItemCategoryService.class);
	
	@Autowired
	ItemCategoryTemplateRepository itemCategoryTemplateRepository;

	@Autowired
	ItemCategoryRepository itemCategoryRepository;
	
	@Autowired
	UserService userService;
	
	@Autowired
	MongoTemplate mongoTemplate;

	@Transactional
	public void clonefromReceiptItemCategoryTemplate(AppUser appUserUpdated) {
		// Copying from item category template to user specific item category
		List<ReceiptItemCategoryTemplate> receiptItemCategoryTemplates = itemCategoryTemplateRepository.findAll();
		for (ReceiptItemCategoryTemplate receiptItemCategoryTemplate : receiptItemCategoryTemplates) {
			
			ReceiptItemCategory receiptItemCategory = new ReceiptItemCategory();
			receiptItemCategory.setId(null);
			receiptItemCategory.setValue(receiptItemCategoryTemplate.getValue());
			receiptItemCategory.setUserPID(appUserUpdated.getId());
			
			mongoTemplate.save(receiptItemCategory, getCollection(appUserUpdated.getId(), ReceiptItemCategory.class));
			itemCategoryRepository.save(receiptItemCategory);
		}
	}
	public ReceiptItemCategory save(ReceiptItemCategory receiptItemCategory) {
		if(receiptItemCategory.getId()!=null && receiptItemCategory.getId().equals("_empty")) {
			receiptItemCategory.setId(null);
		}
		itemCategoryRepository.save(receiptItemCategory);
		
		mongoTemplate.save(receiptItemCategory, getMyCollection(ReceiptItemCategory.class));
		ReceiptItemCategory myReceiptItemCategoryCreated = mongoTemplate.findOne(new Query(Criteria.where("value").is(receiptItemCategory.getValue())), ReceiptItemCategory.class, getMyCollection(ReceiptItemCategory.class));
		logger.info("ID Generated : " + receiptItemCategory.getId());
		return myReceiptItemCategoryCreated;
	}
	public List<ReceiptItemCategory> findMyCategories(String searchTerm) {
		Query query = new Query(Criteria.where("value").regex("(?i)"+searchTerm));
		List<ReceiptItemCategory> receiptItemCategories = mongoTemplate.find(query, ReceiptItemCategory.class, getMyCollection(ReceiptItemCategory.class));
		if(receiptItemCategories != null) {
			logger.info("No. of receipt Item categories found : " + receiptItemCategories.size());
		}
		return receiptItemCategories;
	}
	
	public void deleteMyReceiptItemCategory(String receiptItemCategoryPID) {
		Query query = new Query(Criteria.where("id").is(receiptItemCategoryPID));
		mongoTemplate.findAndRemove(query, ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
		
	}
	public ReceiptItemCategory findMyReceiptItemCategory(String id) {
		Query query = new Query(Criteria.where("id").is(id));
		return mongoTemplate.findOne(query, ReceiptItemCategory.class, getMyCollection(ReceiptItemCategory.class));
	}
	public List<ReceiptItemCategory> findMyItemCategories() {
		List<ReceiptItemCategory> receiptItemCategories = mongoTemplate.findAll(ReceiptItemCategory.class, getMyCollection(ReceiptItemCategory.class));
		return receiptItemCategories;
	}
	/**
	 * @param value
	 * @param userPID
	 * @return
	 */
	public ReceiptItemCategory getMyReceiptItemCategoryByName(String value) {
		ReceiptItemCategory receiptItemCategory = null;
		Query query = new Query(Criteria.where("value").is(value));
		receiptItemCategory = mongoTemplate.findOne(query, ReceiptItemCategory.class, getMyCollection(ReceiptItemCategory.class));
		return receiptItemCategory;
	}
}
